<html>
<head>
<title>{{ team.name }} - Iron Puzzler {{ game.location }}</title>
<link rel="stylesheet" href="static/style.css">
<script>
// Update points allocated
function calc(puzz_type, score_index) {
  var total = 0.0, num = 0;
  for (num = 0; ; num++) {
    var id = "score." + puzz_type + "." + num + "." + score_index;
    var input = document.getElementById(id);
    if (!input) break;
    score = parseFloat(input.value);
    if (score == NaN || score < 0.0 || score > 5.0) {
      input.style.background = "#FFAAAA";
    } else {
      input.style.background = "";
      total += score;
    }
  }

  var target = num * 3.0;
  var ref = document.getElementById("target." + puzz_type);
  if (ref) ref.innerHTML = target;

  var out = document.getElementById("total." + puzz_type + "." + score_index);
  if (out) {
    var diff = Math.abs(total - target);
    out.value = Math.round(total * 100000.0) / 100000.0;  // Avoid "0.999999999"
    if (diff < 1.0) {
      out.style.background = "#88FF88";
    } else if (diff < 3.0) {
      out.style.background = "#FFFF00";
    } else {
      out.style.background = "#FFAAAA";
    }
  }
}

{% regroup other_puzzles by key_name as puzzle_types %}
function calc_all() {
  {% for type in puzzle_types %}
    {% for score in type.list.0.feedback.scores %}
      calc("{{ type.grouper }}", {{ forloop.counter0 }});
    {% endfor %}
  {% endfor %}
}
</script>
</head><body onload="calc_all();"><div class="pagecontent">

<h2><a href="/">IP {{ game.location }}</a> &raquo; {{ team.name }}</h2>

{% if "set_password" in error %}
  <p class="error">New password confirmation did not match!</p>
{% endif %}

{% if game.voting_enabled or game.results_enabled %}

<h3>Voting</h3>

<form name="voting" action="/team" method="post">
<input type="hidden" name="t" value="{{ team.key_id }}">

<p>Use numeric scores, 0.0-5.0.</p>

<p>Aim for 3.0 average per category -- not required, but your "generosity" will
be published.</p>

<p>Click the puzzle to see the solution or to leave comments for the author.</p>

<table>
{% for type in puzzle_types %}

  <tr><td height="5"> </td></tr>
  <tr>
    <td><b>{{ type.grouper|capfirst }}</b></td>
    <td>Fun</td> <td>Pres.</td> <td>Ingr.</td>
  </tr>

  {% for puzzle in type.list %}
    <tr>
      <td><nobr><a href="/guess?t={{ team.key_id }}&p={{ puzzle.number|urlencode }}">#{{ puzzle.number }} {{ puzzle.title }}</a></nobr></td>
      {% for score in puzzle.feedback.scores %}
        <td>
          <input type="hidden"
                 name="score.{{ puzzle.number }}.{{ forloop.counter0 }}.orig"
                 value="{{ score }}">
          <input type="text" size="3"
                 {% if not game.voting_enabled %} disabled {% endif %}
                 id="score.{{ type.grouper }}.{{ forloop.parentloop.counter0 }}.{{ forloop.counter0 }}"
                 onChange='calc("{{ type.grouper }}", {{ forloop.counter0 }});'
                 name="score.{{ puzzle.number }}.{{ forloop.counter0 }}"
                 value="{{ score }}">
        </td>
      {% endfor %}
    </tr>
  {% endfor %}

  <tr>
    <td>Generosity (aim for <span id="target.{{ type.grouper }}"></span>)</td>
    {% for score in type.list.0.feedback.scores %}
      <td>
        <input type="text" size="3" readonly
               id="total.{{ type.grouper }}.{{ forloop.counter0 }}">
      </td>
    {% endfor %}
  </tr>
  <tr><td height="5"> </td></tr>

{% endfor %}
</table>

{% if game.voting_enabled %}
<p><button type="submit">Save votes</button>
<label>
  <input type="checkbox" name="normalize" value="true">
  Auto-normalize scores to 3.0 average
</p>
{% endif %}
</form>

{% endif %}  {# voting_enabled or results_enabled #}

{% if game.solving_enabled %}

<h3>Solving</h3>

<table>
{% for puzzle in other_puzzles %}
  <tr>
    <td>
      #{{ puzzle.number }} {{ puzzle.title }}
    </td>
    <td>
      {% if puzzle.solve_teams %}
        <span title="{{ puzzle.solve_teams|length }} team{{ puzzle.solve_teams|length|pluralize }} solved #{{ puzzle.number }} &quot;{{ puzzle.title }}&quot;">
          {{ puzzle.solve_teams|length }}
          solve{{ puzzle.solve_teams|length|pluralize }}
        </span>
      {% endif %}
    </td>
    <td>
      <form action="/guess" method="post">
        {% if puzzle.solve_time %}
          <b class="guess_correct">SOLVED</b>
          {{ puzzle.solve_time|timesince }} ago
        {% else %}
          <input type="hidden" name="t" value="{{ team.key_id }}">
          <input type="hidden" name="p" value="{{ puzzle.number }}">
          <input type="text" name="answer" size=20>
          <button type="submit">Submit</button>
        {% endif %}
        {% if puzzle.guess_count %}
          (<a href="/guess?t={{ team.key_id }}&p={{ puzzle.number|urlencode }}">{{ puzzle.guess_count }} attempt{{ puzzle.guess_count|pluralize }}</a>)
        {% endif %}
      </form>
    </td>
  </tr>
{% endfor %}
</table>

<h3>Errata</h3>

{% for puzzle in errata_puzzles|dictsortreversed:"errata_timestamp" %}
  <h4>
    #{{ puzzle.number }} {{ puzzle.title }}
    <em>({{ puzzle.errata_timestamp|timesince }} ago)</em>
  </h4>
  <div style="padding-left: 25px">
    {{ puzzle.errata|linebreaks }}
  </div>
{% empty %}
  <p><em>No puzzles have posted errata yet.</em></p>
{% endfor %}

{% endif %}  {# solving_enabled #}

<h3>Your puzzles</h3>

<table>
  {% for puzzle in team_puzzles %}
    <tr>
      <td>
        <b>
          {% if game.solving_enabled %}
            <a href="/puzzle?t={{ team.key_id }}&p={{ puzzle.number|urlencode }}"
               title="#{{ puzzle.number }} {{ puzzle.title }}">
              {{ puzzle.key_name|capfirst }}</a>
          {% else %}
            <a href="/puzzle?t={{ team.key_id }}&p={{ puzzle.number|urlencode }}">
              #{{ puzzle.number }} {{ puzzle.title }}</a>
            ({{ puzzle.key_name }})
          {% endif %}
        </b>
      </td>

      <td>
        {% if puzzle.solve_teams %}
          {{ puzzle.solve_teams|length }}
          solve{{ puzzle.solve_teams|length|pluralize }}
        {% endif %}
      </td>

      {% if game.results_enabled %}
        <td>
          {% if puzzle.vote_count %}
            {{ puzzle.vote_count }} vote{{ puzzle.vote_count|pluralize }}
          {% endif %}
        </td>
        <td>
          {% if puzzle.comment_count %}
            {{ puzzle.comment_count }}
            comment{{ puzzle.comment_count|pluralize }}
          {% endif %}
        </td>
      {% endif %}

      {% if puzzle.answers %}
        <td class="guess_correct">
          {{ puzzle.answers|length }} answer{{ puzzle.answers|pluralize }}
        </td>
      {% else %}
        <td class="guess_incorrect">Needs answers</td>
      {% endif %}

      {% if puzzle.puzzle_blob and puzzle.solution_blob %}
        <td class="guess_correct">Has files</td>
      {% else %}
        {% if puzzle.key_name == "paper" %}
          <td class="guess_incorrect">Needs file upload</td>
        {% else %}
          <td class="guess_none">File optional</td>
        {% endif %}
      {% endif %}
    </tr>
  {% empty %}
    <tr><td><em>Your puzzles have not been assigned yet.</em></td></tr>
  {% endfor %}
</table>

{% if game.ingredients_visible %}
  <p>Ingredients: <b>{{ game.ingredients|join:", " }}</b></p>
{% endif %}

<h3>Team account</h3>

<form action="/team" method="post">
<input type="hidden" name="t" value="{{ team.key_id }}">

<p>
<label>Team name:
  <input type="text" name="name" value="{{ team.name }}" size=30></label><br>
<label>Contact email:
  <input type="text" name="email" value="{{ team.email }}" size=30></label><br>
<label>Change password:
  <input type="password" name="set_password" size=10></label>
<label>confirm:
  <input type="password" name="confirm_password" size=10></label><br>
</p>

<button type="submit">Update account</button>
</form>
</div></body></html>  <!-- end .pagecontent -->
